package com.situ.yB2C.catalog.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.yB2C.catalog.dao.CatalogDao;
import com.situ.yB2C.catalog.pojo.Catalog;
import com.situ.yB2C.catalog.service.CatalogService;

@Service
public class CatalogServiceImpl implements CatalogService {
	private static final Logger LOG = LoggerFactory.getLogger(CatalogServiceImpl.class);
	
	@Autowired 
	private CatalogDao catalogDao;
	@Override
	public int saveCatalog(Catalog catalog) {
		System.out.println(catalog.toString());
		catalog.setActiveFlag(1);
		catalog.setCreateBy("admin");
		catalog.setCreateDate(new Date());
		return catalogDao.insert(catalog);
	}

	@Override
	public List<Catalog> findAllCatalog() {
		return catalogDao.selectList();
	}

	@Override
	public Catalog findOneById(Long rowId) {
		return catalogDao.selectOneById(rowId);
	}

	@Override
	public int deleteCatalogById(Long rowId) {
		return catalogDao.delete(rowId);
	}

	@Override
	public int updateByCatalogInof(Catalog catalog) {
		Long rowId = catalog.getRowId();
		Catalog editcatalog = catalogDao.selectOneById(rowId);
		editcatalog.setCataName(catalog.getCataName());
		editcatalog.setCataInfo(catalog.getCataInfo());
		
		editcatalog.setActiveFlag(1);
		editcatalog.setUpdateBy("admin");
		editcatalog.setUpdateDate(new Date());
		
		return catalogDao.update(catalog);
	}

	@Override
	public String getNameById(Long rowId) {
		
		return catalogDao.selectOneById(rowId).getCataName();
	}

	@Override
	public List<Catalog> findTreeCatalogList() {
		//查询出所有的一级目录的集合
		List<Catalog>catalogList = catalogDao.selectByParent(Catalog.DEFAULT_PARENT_ID);
		if(catalogList!=null) {
			//遍历一级目录
			for(Catalog catalog :catalogList) {
				//根据一级目录的主键，查询处所对应的二级目录的集合
				Long rowId = catalog.getRowId();
				List<Catalog>childList = catalogDao.selectByParent(rowId);
				//将二级目录的集合设置到一级目录对象中
				catalog.setChildList(childList);
			}
		}
		LOG.debug(catalogList.toString());
		return catalogList;
	}

//	@Override
//	public List<Catalog> findCatalogByParent(Long parentId) {
//		// TODO Auto-generated method stub
//		return null;
//	}

}
